package alkaid.eis.sys.service;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import alkaid.eis.common.exceptions.EntityMissingException;
import alkaid.eis.sys.entity.Option;
import alkaid.eis.sys.repository.LongTextRepository;
import alkaid.eis.sys.repository.OptionRepository;
import lombok.RequiredArgsConstructor;

/**
 * 设置项服务
 *  
 * @author Lucky Yang
 * @since 0.0.1
 */
@Service
@RequiredArgsConstructor
public class OptionService {
    private final OptionRepository optionRepository;
    private final LongTextRepository longTextRepository;

    public Option findById(Long id) {
        return optionRepository.findById(id).orElseThrow(EntityMissingException::new);
    }

    public Option findByCode(String code) {
        return optionRepository.findByCode(code).orElseThrow(EntityMissingException::new);
    }

    @Transactional(rollbackFor = Exception.class)
    public Option update(Long id, String code, String description) {
        Option entity = this.findById(id);
        entity.setCode(code);
        entity.setDescription(description);
        return optionRepository.save(entity);
    }

    @Transactional(rollbackFor = Exception.class)
    public void deleteById(Long id) {
        optionRepository.deleteById(id);
        longTextRepository.deleteByRefId(id);
    }
}
